package com.spring.ribbon;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RefreshScope
public class HelloController {
	@Autowired
    HelloService helloService;
	
	@RequestMapping(value = "say")
    public String say(@RequestParam(value = "name")String name){
        return helloService.hiService(name);
    }
	@RequestMapping("/timeout")
    public String timeout(){
        System.err.println("**休眠***");
		try{
            //睡5秒，网关Hystrix3秒超时，会触发熔断降级操作
            Thread.sleep(5000);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "timeout";
    }
	
	@RequestMapping(value = "/setCookies",method = RequestMethod.GET)
    public  String setCookies(HttpServletRequest request,HttpServletResponse response){
        //HttpServerletRequest 装请求信息类
        //HttpServerletRespionse 装相应信息的类
		Cookie cookie1= null;
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie c : cookies) {
				if (c.getName().equals("sessionId")) {
					cookie1 = c;
					break;
				}
			}
		}
		System.err.println("**************************cookie***************");
		//System.err.println("sessionId="+cookie1.getValue());
		HttpSession session = request.getSession();
		System.err.println("**********************获取用户信息***************");
		System.err.println("account_userId= "+session.getAttribute("account_userId"));
		System.err.println("account_userCode= "+session.getAttribute("account_userCode"));
		System.err.println("account_userName= "+session.getAttribute("account_userName"));
		
		Cookie cookie=new Cookie("sessionId","123456789");
        //cookie.setPath("/aa/aaa/aa");
		//cookie.setDomain("localhost");
		//cookie.setDomain("zzx.com");
        //cookie.setValue("111111111");
       
        //javax.ws.rs.core.Cookie cookie1 = new javax.ws.rs.core.Cookie("flag", "111", "/res", "www.zzx.com");
        response.addCookie(cookie);
        return "获得cookies信息成功";
    }
	@RequestMapping(value = "/setSession",method = RequestMethod.GET)
	public String setSession(HttpServletRequest request,HttpServletResponse response) {
		HttpSession session = request.getSession();
		session.setAttribute("account_userId", "11111");
		session.setAttribute("account_userCode", "4100");
		session.setAttribute("account_userName", "小明");
		return "保存Session成功";
	}
	
	
	//@Value("${name}")
    //private String foo;
	
	//@Value("${zzx}")
	//private String zzx;
	
	//@Value("${name}")
	//private String strName;
	
    @RequestMapping("/hello")
    public String hello() {
       // return "hello " + foo+"  "+zzx+"   **  "+strName;
    	return null;
    }

}
